<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/22
 * Time: 14:16
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Feedback extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $user_create_id = trim(input('user_create_id'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $feedback_status = input('feedback_status');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where = [];
            if(isset($user_create_id) && $user_create_id != ''){
                $where['user_create_id'] = ['like', '%' . $user_create_id . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($feedback_status) && $feedback_status != ''){
                $where['feedback_status'] = $feedback_status;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['feedback_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['feedback_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['feedback_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('feedback')
                ->join('up_u_user', 'feedback_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('feedback')
                ->join('up_u_user', 'feedback_user_id = user_id', 'left')
                ->field('feedback_id, user_create_id, user_username, user_phone, feedback_phone, 
                feedback_content, feedback_time, feedback_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('feedback_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 消息回复
     * @return mixed|\think\response\Json
     */
    public function textReply(){
        $feedback_id = input('feedback_id');
        if(Request::instance()->isPost()){
            $feedback_reply_title = trim(input('feedback_reply_title'), ' ');
            $feedback_reply_content = trim(input('feedback_reply_content'), ' ');
            $update = [
                'feedback_reply_title' => $feedback_reply_title,
                'feedback_reply_content' => $feedback_reply_content,
                'feedback_status' => 2
            ];
            $user = Db::table('up_feedback')->where('feedback_id', $feedback_id)->value('feedback_user_id');
            Db::startTrans();
            try{
                Db::table('up_feedback')->where('feedback_id', $feedback_id)->update($update);
                
                $notice = [
                    'notice_title' => $feedback_reply_title,
                    'notice_content' => $feedback_reply_content,
                    'notice_user' => ',0,' . $user . ',',
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_type' => 2,
                    'notice_time' => date('Y-m-d H:i:s'),
                    'notice_range' => 2,
                    'notice_committee_id' => 0
                ];
                Db::table('up_notice')->insert($notice);
                
                Db::commit();
                return json(['code' => 200, 'msg' => '回复成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => '回复失败']);
            }
        }
        $this->assign('feedback_id', $feedback_id);
        return $this->fetch('feedback/textreply');
    }
    
    /**
     * 电话回复
     * @return \think\response\Json
     */
    public function phoneReply(){
        $feedback_id = input('id');
        $res = Db::table('up_feedback')->where('feedback_id', $feedback_id)->setField('feedback_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function feedbackDel(){
        $feedback_id = input('id');
        $res = Db::table('up_feedback')->delete($feedback_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}